package com.so206.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.so206.plugins.MyCacheManager;
import com.so206.po.SystemCcConfig;
import com.so206.service.SystemCcService;
import com.so206.utils.HttpClientUtils;
import com.so206.utils.IpKit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 验证码页面
 */
@Controller
@RequestMapping("/kaptcha")
public class KaptchaController {

    @Autowired
    private SystemCcService ccService;

    @Autowired
    private MyCacheManager myCacheManager;


    /**
     * 验证码页面
     */
    @RequestMapping("/code")
    public String code(String back, HttpSession session, Model model, HttpServletRequest request) {
        if (back == null) {
            back = "/";
        }
        session.setAttribute("bak", back);
        model.addAttribute("s", "01");
        String realIp = IpKit.getRealIp(request);
        model.addAttribute("ip", realIp);
        SystemCcConfig config = myCacheManager.get("config");
        if (config == null) {
            config = ccService.getconfig(1L);
            myCacheManager.set("config", config, 60000);
        }
        model.addAttribute("config", config);
        String sign = UUID.randomUUID().toString();
        myCacheManager.set(sign, realIp, 60000);
        model.addAttribute("sign", sign);
        return "code";
    }

    /**
     * 验证ip
     */
    @RequestMapping(value = "/verfy", method = RequestMethod.POST)
    @ResponseBody
    public Map verfy(String vrifyCode, HttpSession session, HttpServletRequest request, String sign) throws Exception {
        HashMap<Object, Object> ret = new HashMap<>();
        String ip = IpKit.getRealIp(request);
        if (sign == null) {
            ret.put("code", -1);
            ret.put("msg", "sign error!");
            return ret;
        }
        String sign_ip = myCacheManager.get(sign);
        if (sign_ip == null) {
            ret.put("code", -1);
            ret.put("msg", "验证超时,请在60秒内完成验证,请刷新页面重试!");
            return ret;
        }
        if (!sign_ip.equals(ip)) {
            ret.put("code", -1);
            ret.put("msg", "IP发生变化,请刷新页面重试!");
            return ret;
        }
        myCacheManager.remove(sign);
        if (vrifyCode == null) {
            ret.put("code", -1);
            ret.put("msg", "验证失败,请刷新页面重试!");
            return ret;
        }
        if (vrifyCode.trim().length() == 0) {
            ret.put("code", -1);
            ret.put("msg", "验证失败,请刷新页面重试!");
            return ret;
        }
        String api = "http://api.vaptcha.com/v2/validate";
        SystemCcConfig config = myCacheManager.get("config");
        if (config == null) {
            config = ccService.getconfig(1L);
            myCacheManager.set("config", config, 60000);
        }
        HashMap<String, String> map = new HashMap<>();
        map.put("id", config.getCapVid());
        map.put("secretkey", config.getCapKey());
        map.put("scene", "01");
        map.put("token", vrifyCode);
        map.put("ip", ip);
        String ret_str = HttpClientUtils.postParameters(api, map);
        System.out.println(ret_str);
        JSONObject object = JSON.parseObject(ret_str);
        if (object == null) {
            ret.put("code", -1);
            ret.put("msg", "验证失败,请刷新页面重试!");
            return ret;
        }
        if (!object.getString("success").equals("1")) {
            ret.put("code", -1);
            ret.put("msg", object.getString("msg"));
            return ret;
        }
        myCacheManager.remove(ip);
        myCacheManager.remove("method:" + ip);
        myCacheManager.remove("url:" + ip);
        ret.put("code", 0);
        ret.put("msg", "验证成功,正在跳转...");
        ret.put("ret_url", session.getAttribute("bak") == null ? "/" : session.getAttribute("bak"));
        return ret;
    }

}
